package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import service.FeedFavoriteService;
import util.DateUtils;
import dao.FeedFavoriteDAO;
import domain.FeedFavorite;

/**
 * 收藏订阅Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class FeedFavoriteServiceImpl implements FeedFavoriteService {
	/**
	 * 收藏订阅DAO
	 * */
	private FeedFavoriteDAO feedFavoriteDao = null;

	/**
	 * 设定收藏订阅DAO
	 * */
	public void setFeedFavoriteDao(FeedFavoriteDAO feedFavoriteDao) {
		this.feedFavoriteDao = feedFavoriteDao;
	}

	/**
	 * 根据收藏订阅ID查询收藏订阅信息
	 * @param feedFavoriteId 收藏订阅ID
	 * @return FeedFavorite 收藏订阅
	 */
	public FeedFavorite selectByPrimaryKey(int feedFavoriteId) {
		FeedFavorite feedFavorite = feedFavoriteDao.selectByPrimaryKey(feedFavoriteId);
		return feedFavorite;
	}

	/**
	 * 根据条件查询收藏订阅数量
	 * @param paramFeedFavorite 收藏订阅
	 * @return int 收藏订阅数量
	 */
	public int countByCriteria(FeedFavorite paramFeedFavorite) {
		// 根据条件查询收藏订阅数量
		paramFeedFavorite = getConditions(paramFeedFavorite);
		int count = feedFavoriteDao.countByCriteria(paramFeedFavorite);
		return count;
	}

	/**
	 * 根据条件查询收藏订阅列表带分页
	 * @param paramFeedFavorite 收藏订阅
	 * @return List 收藏订阅列表
	 */
	public List<FeedFavorite> selectByCriteriaWithBLOBsForPaging(FeedFavorite paramFeedFavorite) {

		// 取得feedFavoriteList
		paramFeedFavorite = getConditions(paramFeedFavorite);
		List<FeedFavorite> feedFavoriteList = feedFavoriteDao.selectByCriteriaForPaging(
				paramFeedFavorite, paramFeedFavorite.getStart(), paramFeedFavorite.getLimit());
		if (feedFavoriteList != null) {
			List<FeedFavorite> resultList = new ArrayList<FeedFavorite>();
			for (FeedFavorite feedFavorite : feedFavoriteList) {
				// 日期
				feedFavorite.setUpdatedStr(DateUtils.formatDate2Str(feedFavorite.getUpdated(),DateUtils.DATE_TIME_PATTON_2));
				resultList.add(feedFavorite);
			}
			return resultList;
		}
		return null;
	}


	/**
	 * 添加收藏订阅
	 * @param feedFavorite 收藏订阅
	 */
	public void insert(FeedFavorite feedFavorite) {
		// 删除区分：未删除
		feedFavorite.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		feedFavorite.setCreateUser(SessionUtils.getCurrentUserId());
		feedFavorite.setCreateTime(sysdate);
		feedFavorite.setUpdateUser(SessionUtils.getCurrentUserId());
		feedFavorite.setUpdateTime(sysdate);
		feedFavoriteDao.insert(feedFavorite);
	}


	/**
	 * 删除收藏订阅
	 * @param feedFavoriteIds 收藏订阅的ID数组 
	 */
	public void delete(String feedFavoriteIds) {
		String[] feedFavoriteIdArr = feedFavoriteIds.split(",");
		for (int i = 0; i < feedFavoriteIdArr.length; i++) {
			feedFavoriteDao.deleteByPrimaryKey(Integer.parseInt(feedFavoriteIdArr[i]));
		}
	}
	
	/**
	 * 设置公共参数
	 * @param paramFeedFavorite 参数对象
	 */
	private FeedFavorite getConditions(FeedFavorite paramFeedFavorite){
		paramFeedFavorite.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return paramFeedFavorite;
	}
}
